package com.boilerCloud.controller.msg;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.msg.PushMessages;
import com.boilerCloud.dto.sys.ReqResult;
import com.boilerCloud.dto.user.AppUser;
import com.boilerCloud.service.msg.IPushService;



@RestController
@RequestMapping("/push/")
public class PushMessageControler {
	private static final Logger log = LoggerFactory.getLogger(PushMessageControler.class);
	@Resource
	private IPushService service;


	@RequestMapping(value = "pushList")
	public ModelAndView accountList() {
		return new ModelAndView("/views/message/pushMessage.html");
	}
	

	/**
	 * 查询推送消息列表
	 * @param grade
	 * @param yema
	 * @param length
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "qryPushMsgList", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult qryDeviceList(@ModelAttribute PushMessages device ,
			@RequestParam(required = true) int yema,
			@RequestParam(required = true) int length,HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			
			HzlqswPage<PushMessages> page = new HzlqswPage<PushMessages>();
			page.setCurPage(yema);
			page.setPageSize(length);
			service.qryList(device,page);
			rst.setReturnObject(page);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("查询推送消息列表失败");
			log.error("查询推送消息列表异常",e);
			return rst;
		}
	}
	
	
	/**
	 * 新增推送消息
	 * @param device
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addPushMessages", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult addPushMessages(
			@ModelAttribute PushMessages msg ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			service.addPushMessages(msg);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("新增推送消息信息失败");
			log.error("新增推送消息信息异常",e);
			return rst;
		}
	}
	
	
	
	/**
	 * 获取接收人
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getReceiverList", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult getDeviceInfo(
			
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			List<AppUser> list = service.getReceiverList();
			rst.setResultCode(ReqResult.resultCode_Success);
			rst.setReturnObject(list);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("删除推送消息失败");
			log.error("删除推送消息异常",e);
			return rst;
		}
	}
	

	@RequestMapping(value = "delMsg", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult delMsg(
			@RequestParam(required = true) int id,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			service.del(id);
			
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("删除设备失败");
			log.error("删除设备异常",e);
			return rst;
		}
	}
	

	

}
